{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python基础语法复习\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "micafunginmicafunginmicafungin\n",
      "True\n",
      "Micafungin\n",
      "10\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# 环境\n",
    "\n",
    "# 字符串重复输出\n",
    "string = \"micafungin\"\n",
    "print(3*string)\n",
    "# in运算符\n",
    "print('f'in string)\n",
    "# 大写\n",
    "print(string.capitalize())\n",
    "# 长度\n",
    "print(len(string))\n",
    "# 定位\n",
    "str2=\"fun\"\n",
    "print(string.find(str2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['runc', 'containerd', 'docker', 'k8s']\n",
      "['runc', 'containerd', 'docker']\n"
     ]
    }
   ],
   "source": [
    "# 列表\n",
    "list1 = ['runc','containerd','docker']\n",
    "list1=list1+['k8s']\n",
    "print(list1)\n",
    "del list1[3]\n",
    "print(list1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tuple 的元素不能直接修改\n",
    "t = ('a','b','c')\n",
    "t[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 4}\n",
      "{0, 2, 4}\n"
     ]
    }
   ],
   "source": [
    "# 集合set\n",
    "s1=set()\n",
    "s2={0,1,2}\n",
    "s2.add(4)\n",
    "print(s2)\n",
    "s2.remove(1)\n",
    "print(s2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Alice': 12, 'Bob': 14, 'Carol': 17, 'Dave': 19}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典\n",
    "dict1={'name':'Alice','age':18}\n",
    "# dict from list\n",
    "name = [\"Alice\",\"Bob\",\"Carol\",\"Dave\"]\n",
    "age = [12,14,17,19]\n",
    "\n",
    "dict2=dict(zip(name,age))\n",
    "dict2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# lambda 函数\n",
    "add= lambda a,b:a+b\n",
    "add(5,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A simple, lightweight distribution\n",
      "\n",
      "You've reached the website for Arch Linux, a lightweight and flexible Linux® distribution that tries to Keep It Simple.\n",
      "\n",
      "Currently we have official packages optimized for the x86-64 architecture. We complement our official package sets with a community-operated package repository that grows in size and quality each and every day.\n",
      "\n",
      "Our strong community is diverse and helpful, and we pride ourselves on the range of skillsets and uses for Arch that stem from it. Please check out our forums and mailing lists to get your feet wet. Also glance through our wiki if you want to learn more about Arch.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function TextIOWrapper.close()>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f=open(\"text.txt\",'r',encoding='utf-8')\n",
    "print(f.read())\n",
    "f.close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "line 1 A simple, lightweight distribution\n",
      "\n",
      "line 2 \n",
      "\n",
      "line 3 You've reached the website for Arch Linux, a lightweight and flexible Linux® distribution that tries to Keep It Simple.\n",
      "\n",
      "line 4 \n",
      "\n",
      "line 5 Currently we have official packages optimized for the x86-64 architecture. We complement our official package sets with a community-operated package repository that grows in size and quality each and every day.\n",
      "\n",
      "line 6 \n",
      "\n",
      "line 7 Our strong community is diverse and helpful, and we pride ourselves on the range of skillsets and uses for Arch that stem from it. Please check out our forums and mailing lists to get your feet wet. Also glance through our wiki if you want to learn more about Arch.\n"
     ]
    }
   ],
   "source": [
    "i=0\n",
    "with open(\"text.txt\",'r') as fp:\n",
    "    for line in fp.readlines():\n",
    "        i=i+1\n",
    "        print(\"line\",i,line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# write\n",
    "fp=open('text2.txt','w')\n",
    "str = \"Arch is best\"\n",
    "fp.write(str)\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 面向对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age 12 Name Alice\n"
     ]
    }
   ],
   "source": [
    "class Student(object):\n",
    "    count = 0\n",
    "    def __init__(self,name,age):\n",
    "        self.name=name\n",
    "        self.age=age\n",
    "    def out(self):\n",
    "        print(self.name)\n",
    "        print(self.age)\n",
    "    @classmethod    #类方法\n",
    "    def getCount(cls):\n",
    "        print(cls.count)\n",
    "stu1=Student('Alice',12)\n",
    "print(\"Age\",stu1.age,\"Name\",stu1.name)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 继承\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
